/*
 * Copyright © 2010 - 2012 Modo Labs Inc. All rights reserved.
 *
 * The license governing the contents of this file is located in the LICENSE
 * file located at the root directory of this distribution. If the LICENSE file
 * is missing, please contact sales@modolabs.com.
 *
 */

var map;
var mapLoader;

///// window size

// ie7 doesn't understand window.innerWidth and window.innerHeight
function getWindowHeight() {
    if (window.innerHeight !== undefined) {
        return window.innerHeight;
    } else {
        return document.documentElement.clientHeight;
    }
}

function getWindowWidth() {
    if (window.innerWidth !== undefined) {
        return window.innerWidth;
    } else {
        return document.documentElement.clientWidth;
    }
}

// assuming only one of updateMapDimensions or updateContainerDimensions
// gets used so they can reference the same ids
// updateMapDimensions is called for static maps
// updateContainerDimensions is called for dynamic maps
var updateMapDimensionsTimeoutIds = [];
function clearUpdateMapDimensionsTimeouts() {
    for(var i = 0; i < updateMapDimensionsTimeoutIds.length; i++) {
        window.clearTimeout(updateMapDimensionsTimeoutIds[i]);
    }
    updateMapDimensionsTimeoutIds = [];
}

// resizing counterpart for dynamic maps
function updateContainerDimensions() {
    if (typeof doUpdateContainerDimensions == 'function') {
        clearUpdateMapDimensionsTimeouts();
        //var timeoutId = window.setTimeout(doUpdateContainerDimensions, 200);
        //updateMapDimensionsTimeoutIds.push(timeoutId);
        //var timeoutId = window.setTimeout(doUpdateContainerDimensions, 500);
        //updateMapDimensionsTimeoutIds.push(timeoutId);
        var timeoutId = window.setTimeout(doUpdateContainerDimensions, 1000);
        updateMapDimensionsTimeoutIds.push(timeoutId);
    }
}

function findPosY(obj) {
// Function for finding the y coordinate of the object passed as an argument.
// Returns the y coordinate as an integer, relative to the top left origin of the document.
    var intCurlTop = 0;
    if (obj.offsetParent) {
        while (obj.offsetParent) {
            intCurlTop += obj.offsetTop;
            obj = obj.offsetParent;
        }
    }
    return intCurlTop;
}

KGOMapLoader.prototype.generateInfoWindowContent = function(attribs) {
    var content = '';
    if ("title" in attribs && attribs["title"] !== null) {
        content += '<div class="map_name">' + attribs["title"] + '</div>';
    }
    if ("subtitle" in attribs && attribs["subtitle"] !== null) {
        content += '<div class="smallprint map_address">' + attribs["subtitle"] + '</div>';
    }
    content += '<div class="calloutTail"></div>';

    var div = document.createElement("div");
    div.className = "calloutMain";
    var a = null;

    if ("url" in attribs && attribs["url"] !== null) {
        a = document.createElement("a");
        a.href = attribs["url"];
    }

    if ("onclick" in attribs) {
        if (!a) {
            a = document.createElement("a");
        }
        a.onclick = attribs["onclick"];
    }

    if (a) {
        div.appendChild(a);
        a.innerHTML = content;
    } else {
        div.innerHTML = content;
    }
    return div;
}
